package com.cardone.platform.controller;

import java.util.Date;

import lombok.extern.slf4j.Slf4j;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.context.request.NativeWebRequest;

import com.cardone.context.ContextHolder;

/**
 * 高级配置
 *
 * @author yaohaitao
 */
@ControllerAdvice
@Slf4j
public class AdviceController {
	@InitBinder
	public void initBinder(final WebDataBinder webDataBinder) {
		webDataBinder.registerCustomEditor(Date.class, ContextHolder.getBean(com.cardone.common.beans.propertyeditors.CustomDateEditor.class));
	}

	@ExceptionHandler(Throwable.class)
	@ResponseStatus(HttpStatus.UNAUTHORIZED)
	public String processUnauthenticatedException(final NativeWebRequest request, final Throwable e) {
		AdviceController.log.error(e.getMessage(), e);

		return "default/error/500";
	}
}
